package com.itheima.manage.service;

import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.commons.pojo.entity.VideoInfo;
import com.itheima.commons.pojo.mongo.Video;
import com.itheima.dubbo.interfaces.CommentApi;
import com.itheima.dubbo.interfaces.VideoApi;
import com.itheima.dubbo.interfaces.VideoInfoApi;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wds
 * @date 2021/12/20
 * @apiNote
 */
@Service
public class VideoService {

    @Reference
    VideoInfoApi videoInfoApi;

    @Reference
    VideoApi videoApi;

    @Reference
    CommentApi commentApi;

    /**
     * 建表
     */
    public void saveVideoInfo() {
        List<Video> videoList = videoApi.findAll();

        for (Video video : videoList) {
            VideoInfo videoInfo = new VideoInfo();
            // 不传自增
            // publishInfo.setId(0L);
            videoInfo.setVideoId(video.getId().toHexString());
            // 默认机器审核通过
            videoInfo.setState("2");
            // 默认未置顶
            videoInfo.setIstop(1);
            videoInfo.setUserId(video.getUserId());
            videoInfo.setReportCount(0);
            videoInfo.setLikeCount(Convert.toInt(commentApi.queryLikeCount(video.getId().toHexString()), 0));
            videoInfo.setCommentCount(Convert.toInt(commentApi.queryCommentCount(video.getId().toHexString()), 0));
            videoInfo.setForwardingCount(Convert.toInt(commentApi.queryLoveCount(video.getId().toHexString()), 0));
            videoInfo.setCreated(video.getCreated());
            videoInfo.setUpdated(video.getCreated());

            videoInfoApi.saveVideoInfo(videoInfo);
        }

    }
}
